package com.nep.NEPM.service;

import com.nep.NEPM.Data.AQILevelData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class AqiDataService {

    public ObservableList<AQILevelData> loadAqiDataFromResource() {
        ObservableList<AQILevelData> data = FXCollections.observableArrayList();

        try (InputStream is = getClass().getResourceAsStream("/data/AQI_data.csv");
             BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {

            String line = reader.readLine();

            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 3) {
                    data.add(new AQILevelData(
                            parts[0].trim(),
                            parts[1].trim(),
                            Integer.parseInt(parts[2].trim())
                    ));
                }
            }
        } catch (Exception e) {
            System.err.println("Error loading AQI data: " + e.getMessage());
            e.printStackTrace();
        }

        return data;
    }
}